package com.indusfo.spc.service.impl;

import com.indusfo.spc.exception.GlobalException;
import com.indusfo.spc.exception.ModifyFailedException;
import com.indusfo.spc.exception.ParamsErrorException;
import com.indusfo.spc.mapper.RoleUserMapper;
import com.indusfo.spc.pojo.RoleUser;
import com.indusfo.spc.service.RoleUserService;
import com.indusfo.spc.vo.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户角色授权业务层的接口
 *
 * @author 贺闻博
 * <p>
 * 2018年11月30日
 */
@Service
public class RoleUserServiceImpl implements RoleUserService {
    private static final Logger logger = LoggerFactory.getLogger(RoleUserServiceImpl.class);
    @Resource
    private RoleUserMapper roleUserMapper;

    /**
     * 查询单个角色授权业务层
     */
    @Override
    public JSONObject selectRoleUser(RoleUser roleUser) {
        List<RoleUser> ListRoleUser = null;
        try {
            Integer lRoleUserId = roleUser.getUserId();

            if (lRoleUserId == null) {
                throw new ModifyFailedException("用户Id不能为空");
            }

            ListRoleUser = roleUserMapper.selectRoleUser(lRoleUserId);
            if (ListRoleUser.isEmpty()) {
                throw new ModifyFailedException("没有用户的授权信息");
            }
            return JSONObject.oK("查询成功", ListRoleUser, ListRoleUser.size());
        } catch (GlobalException e) {
            logger.error(e.getMessage(), e);
            return JSONObject.build(500, e.getMessage());
        }
    }

    /**
     * 根据角色id查询用户信息
     *
     * @param roleUser
     * @return
     */
    @Override
    public JSONObject selectRoleId(RoleUser roleUser) {
        List<RoleUser> ListRoleUser = null;
        try {
            Integer lRoleId = roleUser.getRoleId();

            if (lRoleId == null) {
                throw new ModifyFailedException("角色Id不能为空");
            }

            ListRoleUser = roleUserMapper.selectRoleId(lRoleId);
            if (ListRoleUser.isEmpty()) {
                return JSONObject.oK("没有用户的授权信息", ListRoleUser, ListRoleUser.size());
            }
            return JSONObject.oK("查询成功", ListRoleUser, ListRoleUser.size());
        } catch (GlobalException e) {
            logger.error(e.getMessage(), e);
            return JSONObject.build(500, e.getMessage());
        }
    }

    /**
     * 更新数据业务接口
     */
    @Override
    public JSONObject updateRoleUser(RoleUser roleUser) {
        try {
            Integer lUserId = roleUser.getUserId();
            Integer lRoleId = roleUser.getRoleId();
            Integer lFightFlag = roleUser.getFightFlag();
            if (lUserId == null) {
                throw new ModifyFailedException("用户Id不能为空");
            }
            if (lRoleId == null) {
                throw new ModifyFailedException("角色Id不能为空");
            }
            if (lFightFlag == null) {
                throw new ModifyFailedException("授权信息不能为空");
            }

            // 保存数据
            int num = roleUserMapper.updateByPrimaryKeySelective(roleUser);
            if (num == 0) {
                throw new ModifyFailedException("更新数据失败");
            }
            return JSONObject.build(200, "更新数据成功", num);

        } catch (GlobalException e) {
            logger.error(e.getMessage(), e);
            return JSONObject.build(500, e.getMessage());
        }
    }

    /**
     * 新增接口
     */
    @Override
    public JSONObject insertRoleUser(RoleUser roleUser) {
        try {
            Integer lUserId = roleUser.getUserId();
            Integer lRoleId = roleUser.getRoleId();
            Integer lFightFlag = roleUser.getFightFlag();
            if (lUserId == null) {
                throw new ModifyFailedException("用户Id不能为空");
            }
            if (lRoleId == null) {
                throw new ModifyFailedException("角色Id不能为空");
            }
            if (lFightFlag == null) {
                throw new ModifyFailedException("授权信息不能为空");
            }
            int num = roleUserMapper.insertSelective(roleUser);
            if (num == 0) {
                throw new ModifyFailedException("新增数据失败");
            }
            return JSONObject.build(200, "新增数据成功", num);

        } catch (GlobalException e) {
            logger.error(e.getMessage(), e);
            return JSONObject.build(500, e.getMessage());
        }
    }

    /**
     * 修改状态接口  (启用 停用 和删除 )
     */
    @Override
    public JSONObject deleteRoleUser(Long[] lUserId, Integer lRoleId, Integer lDataState) {
        try {
            if (lUserId == null) {
                throw new ModifyFailedException("用户Id不能为空");
            }
            if (lRoleId == null) {
                throw new ModifyFailedException("角色Id不能为空");
            }
            if (lDataState == null) {
                throw new ModifyFailedException("数据状态不能为空");
            }
            int num = roleUserMapper.deleteRoleUser(lUserId, lRoleId, lDataState);
            String msg = "";
            if (num == 0) {
                switch (lDataState) {
                    case 1:
                        throw new ModifyFailedException("启用失败");
                    case 2:
                        throw new ModifyFailedException("删除失败");
                    case 3:
                        throw new ModifyFailedException("停用失败");
                }
            } else {
                switch (lDataState) {
                    case 1:
                        msg = "启用成功";
                        break;
                    case 2:
                        msg = "删除成功";
                        break;
                    case 3:
                        msg = "停用成功";
                        break;
                }
            }
            return JSONObject.oK(msg);

        } catch (GlobalException e) {
            logger.error(e.getMessage(), e);
            return JSONObject.build(500, e.getMessage());
        }
    }

    /**
     * 将数据永久删除接口(non-Javadoc)
     *
     * @author hewb
     * @date 2018年12月26日, 上午10:41:02
     * @see com.indusfo.spc.service.RoleUserService#deleteRoleUserAll(Integer)
     */
    @Override
    public JSONObject deleteRoleUserAll(Integer lRoleId) {
        try {
            if (lRoleId == null) {
                throw new ModifyFailedException("角色id不能为空");
            }
            roleUserMapper.deleteRoleUserAll(lRoleId);
            return JSONObject.oK();
        } catch (GlobalException e) {
            logger.error(e.getMessage(), e);
            return JSONObject.build(500, e.getMessage());
        }
    }

    /**
     * 根据角色ID删除用户角色关联信息，逻辑删除
     *  
     * @author xuz
     * @date 2019/7/9 4:59 PM
     * @param lRoleIds, lDataState]
     * @return void
     */
    @Override
    public void deleteRoleUserByRoleId(Long[] lRoleIds, Integer lDataState) {
        try {
            if (lRoleIds == null) {
                throw new ModifyFailedException("角色Id不能为空");
            }
            if (lDataState == null) {
                throw new ModifyFailedException("数据状态不能为空");
            }
            int num = roleUserMapper.deleteRoleUserByRoleId(lRoleIds, lDataState);
            String msg = "";
            if (num == 0) {
                switch (lDataState) {
                    case 1:
                        throw new ModifyFailedException("启用失败");
                    case 2:
                        throw new ModifyFailedException("删除失败");
                    case 3:
                        throw new ModifyFailedException("停用失败");
                }
            } else {
                switch (lDataState) {
                    case 1:
                        msg = "启用成功";
                        break;
                    case 2:
                        msg = "删除成功";
                        break;
                    case 3:
                        msg = "停用成功";
                        break;
                }
            }

        } catch (GlobalException e) {
            logger.error(e.getMessage(), e);
        }
    }

    //---------------------------------- begin --------------------------------------//
    /**
     * 通过角色ID查询用户权限集合
     *
     * @author xuz
     * @date 2019/7/15 5:03 PM
     * @param lRoleId
     * @return java.util.List<com.indusfo.spc.pojo.RoleUser>
     */
    @Override
    public List<RoleUser> queryRoleUserByRoleId(Integer lRoleId) {
        if (null==lRoleId) {
            throw new ParamsErrorException("角色ID不能为空");
        }
        List<RoleUser> roleUserList = roleUserMapper.selectRoleId(lRoleId);
        return roleUserList;
    }

    //---------------------------------- end --------------------------------------//
}
